package fm.dongman.animefm.view.fragment

import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast

import fm.dongman.animefm.R
import fm.dongman.animefm.contractlib.IModel
import fm.dongman.animefm.contractlib.contract.ChatListContract
import fm.dongman.animefm.view.adapter.ChatListAdapter
import fm.dongman.animefm.view.util.ActivityHelper
import fm.dongman.animefm.view.util.SystemHelper
import fm.dongman.animefm.view.util.ToastHelper
import fm.dongman.animefm.view.widget.MultiStateLayout
import fm.dongman.modellib.StaticArea.Injection
import fm.dongman.presenterlib.presenter.ChatListPresenter
import kotlinx.android.synthetic.main.fragment_chat_list.*

/**
 * 聊天信息列表页面，类似 QQ 消息列表
 * Create by Administrator on 2017/10/26.
 */
class ChatListFragment : BaseFragment(), ChatListContract.IChatListView {
    private lateinit var mPresenter: ChatListContract.IChatListPresenter // 全局数据控制器

    private lateinit var mChatListAdapter: ChatListAdapter  // 私信列表适配器
    private val mChatList: ArrayList<IModel.IMsgModel> = ArrayList() // 私信数据集合

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val view = inflater!!.inflate(R.layout.fragment_chat_list, container, false)
        ChatListPresenter(this, Injection.provideChatListRepository())
        return view
    }

    override fun initData() {
        onStateChanged(MultiStateLayout.LOADING)
        mPresenter.startLoad()
    }

    override fun bindData() {
        bindToolbar()
        bindChatList()
    }

    /**
     * 绑定 Toolbar
     */
    private fun bindToolbar() {
        mChatListToolbar.setPadding(0, SystemHelper.getStatusBarHeight(activity), 0, 0)
        mChatListToolbar.setNavigationOnClickListener {
            ActivityHelper.backActivity(activity)
        }

    }

    /**
     * 绑定私信列表
     */
    private fun bindChatList() {
        mChatListAdapter = ChatListAdapter(activity, mChatList)
        mChatListRecycler.adapter = mChatListAdapter
        mChatListRecycler.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
    }

    override fun setPresenter(presenter: ChatListContract.IChatListPresenter) {
        mPresenter = presenter
    }

    override fun showNoNetwork() {
        onStateChanged(MultiStateLayout.NET_ERROR)
    }

    override fun showRefreshFailed() {
        ToastHelper.getInstance(activity).showError("聊天信息列表页面"+getString(R.string.str_refresh_failed))
        onStateChanged(MultiStateLayout.EMPTY)
    }

    override fun showChatList(chats: List<IModel.IMsgModel>) {
        onStateChanged(MultiStateLayout.SUCCESS)
        mChatList.clear()
        mChatList.addAll(chats)
        mChatListAdapter.notifyDataSetChanged()
    }

    override fun isActive(): Boolean = isAdded

    override fun getViewContext(): Context = activity

    /**
     * 改变状态
     * @param state 状态值
     */
    private fun onStateChanged(state: Int) {
        mChatListMultiState.changeState(state)
    }

    companion object {
        fun getInstance(): ChatListFragment {
            return ChatListFragment()
        }
    }

    /**
     * 私信列表适配器
     */
//    private class ChatListAdapter(private val mContext: Context, private val mData: ArrayList<IModel.IMsgModel>)
//        : RecyclerView.Adapter<ChatListAdapter.ChatListViewHolder>() {
//
//        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ChatListViewHolder =
//                ChatListViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_chat_list, null))
//
//        override fun onBindViewHolder(holder: ChatListViewHolder?, position: Int) {
//
//        }
//
//        override fun getItemCount(): Int = mData.size
//
//        private class ChatListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
//        }
//    }
//

}
